package com.gylang.crawler.handler;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author gylang
 * data 2021/5/26
 */
@Component
public class GetAllMovieIdHandler {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    public List<String> movieIdList() throws IOException {

        SearchRequest searchRequest = new SearchRequest();
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.fetchSource("id", null);
        sourceBuilder.size(10000);
        searchRequest.source(sourceBuilder);
        System.out.println(sourceBuilder.toString());
        SearchResponse searchResponse = restTemplate.getClient().search(searchRequest, RequestOptions.DEFAULT);

        SearchHit[] hits = searchResponse.getHits().getHits();
        return Arrays.stream(hits).map(SearchHit::getId).collect(Collectors.toList());
    }
}
